package com.letoken.platform.repo.awss3.service;

import com.alibaba.fastjson2.JSON;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.letoken.platform.pub.configuration.AwsS3Configuration;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider;
import software.amazon.awssdk.profiles.ProfileFileSupplier;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.rekognition.RekognitionClient;
import software.amazon.awssdk.services.rekognition.model.*;
import software.amazon.awssdk.services.sqs.SqsClient;
import software.amazon.awssdk.services.sqs.model.DeleteMessageRequest;
import software.amazon.awssdk.services.sqs.model.Message;
import software.amazon.awssdk.services.sqs.model.ReceiveMessageRequest;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.Objects;

/**
 * @author wyl
 * @Description
 * @date 2022/12/27 17:46
 * @Version 1.0
 */
@Service
@Slf4j
public class AWSS3Service {

    @Resource
    private AmazonS3 amazonS3;// = new AmazonS3Client();

    @Autowired
    private AWSS3Service aWSS3Service;

    @Resource
    private AwsS3Configuration awsS3Configuration;


    private static RekognitionClient rekClient;
    private static SqsClient sqs;
    private static NotificationChannel channel;





    public static byte[] subBytes(byte[] src, int begin, int count) {
        byte[] bs = new byte[count];
        for (int i = begin; i < begin + count; i++) {
            bs[i - begin] = src[i];
        }
        return bs;
    }


    public String uploadFile(MultipartFile file, String key) {
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentType(file.getContentType());
        objectMetadata.setContentLength(file.getSize());
        String type = Objects.requireNonNull(file.getOriginalFilename()).substring(file.getOriginalFilename().lastIndexOf("."));
        key = key + type;
        //配置文件访问权限
        try {
            amazonS3.putObject(new PutObjectRequest(awsS3Configuration.getBucketName(), key, file.getInputStream(), objectMetadata)
                    .withCannedAcl(CannedAccessControlList.PublicRead));
            //获取一个request
            GeneratePresignedUrlRequest urlRequest = new GeneratePresignedUrlRequest(
                    awsS3Configuration.getBucketName(), key);
            URL url = amazonS3.generatePresignedUrl(urlRequest);
            return awsS3Configuration.getReturnUrlHost() + url.getPath();

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }



}
